package com.lishao.soccer.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.lishao.soccer.bean.entity.TmGameArrive;
import com.lishao.soccer.bean.entity.TmGameJudge;
import com.lishao.soccer.bean.entity.TmGameScore;
import com.lishao.soccer.bean.entity.TmGameSignup;
import com.lishao.soccer.bean.entity.TmGameTech;
import com.lishao.soccer.bean.entity.TmTeamGame;
import com.lishao.soccer.bean.entity.TmTeamPlace;
import com.lishao.soccer.bean.form.TmTeamGameForm;
import com.lishao.soccer.service.TmTeamGameService;
import com.lishao.system.security.providers.SecurityUser;
import com.lishao.system.utils.ActionUtil;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class TeamGameAction extends ActionSupport {
	TmTeamGameService tmTeamGameService;
	
	Long teamId;
	Long tmGameId;
	String signupFlag;//报名
	String playFlag;
	String remark;//报名
	String friendQuantity;//报名
	Long placeId;
	String successFlag;
	String jsonStr;
	Integer ourScore;
	Integer oppScore;
	Long arriveUserId;
	
	public String selectGameSignup() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();

		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		
		TmGameSignup gameSignup = tmTeamGameService.selectGameSignup(teamId, tmGameId, secUser.getSmUser().getRowId());
		
		Map resMap = new HashMap();
		resMap.put("gameSignup", gameSignup);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	
	public String viewGameSignupList() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();

		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		
		List<TmGameSignup> signupList = tmTeamGameService.selectGameSignup(teamId, tmGameId);
		
		Map resMap = new HashMap();
		resMap.put("signupList", signupList);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	//比赛报名
	public String gameSignUp() throws Exception{
		String strMessage = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();

			HttpSession session = request.getSession();
			SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
			
			TmTeamGame tmTeamGame = tmTeamGameService.selectTeamGameById(tmGameId);
			if("20".equals(tmTeamGame.getStatusFlag())){
				TmGameSignup tmGameSignup = new TmGameSignup();
				tmGameSignup.setTeamId(teamId);
				tmGameSignup.setTmGameId(tmGameId);
				tmGameSignup.setSignupUserId(secUser.getSmUser().getRowId());
				tmGameSignup.setSignupFlag(signupFlag);
				tmGameSignup.setPlayFlag(playFlag);
				tmGameSignup.setSignupTime(null);
				tmGameSignup.setRemark(remark);
				tmGameSignup.setFriendQuantity(friendQuantity);
				tmTeamGameService.gameSignUp(tmGameSignup);
				
				strMessage = "报名成功!";
			}else{
				strMessage = "报名失败，当前比赛状态不允许报名!";
			}
			
			
		} catch (Exception e) {
			strMessage = "报名失败!";
			e.printStackTrace();
		}
		
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	
	public String getTeamPlace() throws Exception{
		TmTeamPlace tmTeamPlace = tmTeamGameService.getTeamPlace(placeId);
		Map resMap = new HashMap();
		resMap.put("tmTeamPlace", tmTeamPlace);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	
	public String selectPlaceByTeamId() throws Exception{
		List<TmTeamPlace> placeList = tmTeamGameService.selectPlaceByTeamId(teamId);
		Map resMap = new HashMap();
		resMap.put("placeList", placeList);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	
	public String gameKq() throws Exception{
		String strMessage = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();

			HttpSession session = request.getSession();
			SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
			
			TmGameArrive tmGameArrive = new TmGameArrive();
			tmGameArrive.setTeamId(teamId);
			tmGameArrive.setTmGameId(tmGameId);
			tmGameArrive.setArriveUserId(secUser.getSmUser().getRowId());
			tmGameArrive.setArriveTime(null);
			tmGameArrive.setSuccessFlag(successFlag);
			tmGameArrive.setRemark(remark);
			tmGameArrive.setCreatedBy(secUser.getSmUser().getRowId());
			tmGameArrive.setOriginFlag("S");//用来标示是不是自己考勤的
			tmTeamGameService.gameKq(tmGameArrive,session);
			
			strMessage = remark;
		} catch (Exception e) {
			strMessage = "签到处理异常!";
			e.printStackTrace();
		}
		
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	
//	public String countGameJudge() throws Exception{
//		HttpServletRequest request = ServletActionContext.getRequest();
//		HttpServletResponse response = ServletActionContext.getResponse();
//
//		HttpSession session = request.getSession();
//		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
//		int count = tmTeamGameService.countGameJudge(teamId, tmGameId, secUser.getSmUser().getRowId());
//		Map resMap = new HashMap();
//		resMap.put("count", count);
//		new ActionUtil().writeMap(resMap);
//		return null;
//	}
	public String selectForGameTech() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();

		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		List <TmGameTech> gameTechList = tmTeamGameService.selectForGameTech(teamId,tmGameId,secUser.getSmUser().getRowId());
		Map resMap = new HashMap();
		resMap.put("gameTechList", gameTechList);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	
	public String gameTechSubmit() throws Exception{
		String strMessage = "";
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		
		TmTeamGame tmTeamGame = tmTeamGameService.selectTeamGameById(tmGameId);
		if("30".equals(tmTeamGame.getStatusFlag())){
			List <TmGameTech> gameTechList = JSONArray.toList(JSONArray.fromObject(jsonStr),new TmGameTech(),new JsonConfig());
			tmTeamGameService.gameTechSubmit(teamId, tmGameId, secUser, gameTechList);
			strMessage="评分成功！";
		}else{
			strMessage="当前比赛状态不允许进行评分！";
		}
		
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	
	public String saveTeamGame() throws Exception{
		String strMessage = "";
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		TmTeamGameForm tmTeamGameForm = (TmTeamGameForm)JSONObject.toBean(JSONObject.fromObject(jsonStr, new JsonConfig()), TmTeamGameForm.class);
		boolean bResult = tmTeamGameService.saveGame(tmTeamGameForm, secUser.getSmUser());
		Map resMap = new HashMap();
		resMap.put("bResult", bResult);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	public String findGame() throws Exception{
		TmTeamGame tmTeamGame = tmTeamGameService.findGame(tmGameId);
		Map resMap = new HashMap();
		resMap.put("tmTeamGame", tmTeamGame);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	public String selectForJudgeAssign() throws Exception{
		List<TmGameJudge> gameJudgeList = tmTeamGameService.selectForJudgeAssign(teamId, tmGameId);
		Map resMap = new HashMap();
		resMap.put("gameJudgeList", gameJudgeList);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	public String selectForAddarrived() throws Exception{
		List<TmGameJudge> gameJudgeList = tmTeamGameService.selectForAddarrived(teamId, tmGameId);
		Map resMap = new HashMap();
		resMap.put("gameJudgeList", gameJudgeList);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	
	public String gameJudgeAssignSubmit()throws Exception{
		String strMessage = "";
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		
		TmTeamGame tmTeamGame = tmTeamGameService.selectTeamGameById(tmGameId);
		if("30".equals(tmTeamGame.getStatusFlag())){
			List <TmGameJudge> gameJudgeList = JSONArray.toList(JSONArray.fromObject(jsonStr),new TmGameJudge(),new JsonConfig());
			tmTeamGameService.gameJudgeAssignSubmit(ourScore,oppScore,teamId, tmGameId, secUser, gameJudgeList);
			strMessage="操作成功！";
		}else{
			strMessage="当前比赛状态不允许进行指定评委！";
		}
		
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	public String addArriveList()throws Exception{
		String strMessage = "";
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		
		TmTeamGame tmTeamGame = tmTeamGameService.selectTeamGameById(tmGameId);
		if("30".equals(tmTeamGame.getStatusFlag())){
			List <TmGameJudge> gameJudgeList = JSONArray.toList(JSONArray.fromObject(jsonStr),new TmGameJudge(),new JsonConfig());
			tmTeamGameService.addArriveList(teamId, tmGameId, secUser, gameJudgeList);
			strMessage="操作成功！";
		}else{
			strMessage="当前比赛状态不允许添加人员！";
		}
		
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	public TmTeamGame selectTeamGameById() throws Exception{
		TmTeamGame tmTeamGame = tmTeamGameService.selectTeamGameById(tmGameId);
		Map resMap = new HashMap();
		resMap.put("tmTeamGame", tmTeamGame);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	//查询比赛得分
	public String selectGameScore() throws Exception{
		List<TmGameScore> gameScoreList = tmTeamGameService.selectGameScore(teamId, tmGameId);
		Map resMap = new HashMap();
		resMap.put("gameScoreList", gameScoreList);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	//删除签到的数据
	public String deleteArriveDataByGameUser() throws Exception{
		String strMessage = "";
		int iResult = tmTeamGameService.deleteArriveDataByGameUser(teamId, tmGameId, arriveUserId);
		if(iResult>0){
			strMessage="操作成功！";
		}else{
			strMessage="处理失败，不能删除球员自己签到的数据！";
		}
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	//get set
	public TmTeamGameService getTmTeamGameService() {
		return tmTeamGameService;
	}

	public void setTmTeamGameService(TmTeamGameService tmTeamGameService) {
		this.tmTeamGameService = tmTeamGameService;
	}
	public String getSignupFlag() {
		return signupFlag;
	}

	public void setSignupFlag(String signupFlag) {
		this.signupFlag = signupFlag;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getFriendQuantity() {
		return friendQuantity;
	}

	public void setFriendQuantity(String friendQuantity) {
		this.friendQuantity = friendQuantity;
	}
	public Long getTeamId() {
		return teamId;
	}
	public void setTeamId(Long teamId) {
		this.teamId = teamId;
	}
	public Long getTmGameId() {
		return tmGameId;
	}
	public void setTmGameId(Long tmGameId) {
		this.tmGameId = tmGameId;
	}
	public String getPlayFlag() {
		return playFlag;
	}
	public void setPlayFlag(String playFlag) {
		this.playFlag = playFlag;
	}

	public Long getPlaceId() {
		return placeId;
	}

	public void setPlaceId(Long placeId) {
		this.placeId = placeId;
	}

	public String getSuccessFlag() {
		return successFlag;
	}

	public void setSuccessFlag(String successFlag) {
		this.successFlag = successFlag;
	}
	
	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}

	public Integer getOurScore() {
		return ourScore;
	}

	public void setOurScore(Integer ourScore) {
		this.ourScore = ourScore;
	}

	public Integer getOppScore() {
		return oppScore;
	}

	public void setOppScore(Integer oppScore) {
		this.oppScore = oppScore;
	}

	public Long getArriveUserId() {
		return arriveUserId;
	}

	public void setArriveUserId(Long arriveUserId) {
		this.arriveUserId = arriveUserId;
	}
	
}
